package com.link.base.base.system.controller;

import com.link.base.base.system.model.ConfigDb;
import com.link.base.base.system.service.ConfigDbService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询系统数据库表（仅包含查询功能）
 *
 * @author LiYang
 * @date 20190820
 */
@Controller
@RequestMapping("/link/configDb/")
public class ConfigDbController extends BasicController<ConfigDb> {

    @Resource
    ConfigDbService configDbService;

    @Override
    public BasicService<ConfigDb> getBasicService() throws Exception {
        return configDbService;
    }

    /**
     * 查询配置的数据源表
     *
     * @author LiYang
     * @date 20190820
     */
    @RequestMapping(value = "/queryTable", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryTable(@JsonParam ConfigDb configDb, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<>(4);
        try {
            List<Map<String, Object>> rows = configDbService.queryMySqlTable(configDb);
            result.put("rows", rows);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", "删除数据出现异常！");
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 查询配置的数据源表的列
     *
     * @author LiYang
     * @date 20190820
     */
    @RequestMapping(value = "/queryColumn", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryColumn(@JsonParam ConfigDb configDb, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<>(4);
        try {
            List<Map<String, Object>> rows = configDbService.queryMySqlTableColumn(configDb);
            result.put("rows", rows);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", "删除数据出现异常！");
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 模糊查询配置的数据源表或列
     *
     * @author LiYang
     * @date 20190820
     */
    @RequestMapping(value = "/fuzzyQuery", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> fuzzyQuery(@JsonParam ConfigDb configDb, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> result = new HashMap<>(4);
        List<Map<String, Object>> rows = null;
        try {
            if (StringUtils.isBlank(configDb.getFuzzyCondition())) {
                rows = configDbService.queryMySqlTable(configDb);
            } else {
                rows = configDbService.fuzzyQueryMySql(configDb);
            }
            result.put("rows", rows);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", "删除数据出现异常！");
            result.put("code", "400");
        }
        return result;
    }
}
